package com.chen.judge.strategy.StrategyImpl;

import com.chen.common.enums.JudgeInfoMessageEnum;
import com.chen.judge.codesandbox.model.JudgeInfo;
import com.chen.judge.codesandbox.model.JudgeResult;
import com.chen.judge.codesandbox.model.JudgeContext;
import com.chen.judge.strategy.JudgeStrategy;
import com.chen.popj.entity.JudgeCase;
import com.chen.popj.entity.JudgeConfig;
import com.chen.popj.entity.Problem;

import java.util.List;

/**
 * 默认判题策略
 */
public class DefaultJudgeStrategy implements JudgeStrategy {

    /**
     * 执行判题
     *
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeResult doJudge(JudgeContext judgeContext) {
        System.out.println("默认沙箱");
//        JudgeInfo judgeInfo = judgeContext.getJudgeInfol();
//        Long memory = judgeInfo.getMemory();
//        Long time = judgeInfo.getTime();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        Problem problem = judgeContext.getProblem();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeInfo judgeInfoResponse = new JudgeInfo();
//        judgeInfoResponse.setMemory(memory);
//        judgeInfoResponse.setTime(time);
        // 先判断沙箱执行的结果输出数量是否和预期输出数量相等
        if (outputList.size() != inputList.size()) {
            judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
//            return judgeInfoResponse;
        }
        // 依次判断每一项输出和预期输出是否相等
        for (int i = 0; i < judgeCaseList.size(); i++) {
            JudgeCase judgeCase = judgeCaseList.get(i);
            if (!judgeCase.getOutput().equals(outputList.get(i))) {
                judgeInfoMessageEnum = JudgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
//                return judgeInfoResponse;
            }
        }
        // 判断题目限制
        // String judgeConfigStr = problem.getJudgeConfig();
        // JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        JudgeConfig judgeConfig = JudgeConfig.builder()
                .timeLimit(problem.getTimeLimit())
                .memoryLimit(problem.getMemoryLimit())
                .build();
        Integer needMemoryLimit = judgeConfig.getMemoryLimit();
        Integer needTimeLimit = judgeConfig.getTimeLimit();
//        if (memory > needMemoryLimit) {
//            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
//            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
//            return judgeInfoResponse;
//        }
//        if (time > needTimeLimit) {
//            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
//            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
//            return judgeInfoResponse;
//        }
//        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
//        return judgeInfoResponse;
        return new JudgeResult();
    }
}
